import Dialog from 'tdesign-miniprogram/dialog';
import Toast from 'tdesign-miniprogram/toast';
import {
	fetchCartGroupData
} from '../../../services/cart/cart';
import {
	logcheckSession,
} from '../../../utils/util';
const app = getApp()
const $api = require('../../../utils/request').Api
Page({
	data: {
		cartGroupData: [],
		seleid: true,
		numquantity: [],
		numdata: '',
		numshuzi: '',
    lrrnu: 0,
    baseImgUrl: app.globalData.baseImgUrl,
	},

	// 调用自定义tabbar的init函数，使页面与tabbar激活状态保持一致
	onShow() {
		// this.getTabBar().init();
		// logcheckSession();
	},

	onLoad() {
		this.refreshData();
		this.dibushuliang()
	},

	refreshData() {
		let login = wx.getStorageSync('login')
		// console.log(login.data.data.memberNumber);
		let vin = wx.getStorageSync('vin')
		$api.shoppingCartList({
			memberNumber: login.data.data.memberNumber,
			page: 1,
			size: 10000,
			source: "2",
		vin: vin || '1234567890',
		}).then((res) => {
			console.log(res.data.data.shoppingCartList);
			let isEmpty = true;

			if (res.data.data.shoppingCartList.length > 0) {
				const cartGroupData = res.data.data.shoppingCartList
				// store.isSelected = true; // 该门店已加购商品是否全选
				// store.storeStockShortage = false; // 该门店已加购商品是否存在库存不足
				// 统计是否全选
				for (let item of cartGroupData) {
					if (!item.selected) {
						item.selected = false;
					}
					// console.log(typeof item.activityStatus);
					if (item.activityStatus === '1') {
						item.lopt1 = (item.goodsNum * item.discountPrice).toFixed(2)
						// item.lopt1.toFixed(2)
						// Number(item.lopt1)
					} else if (item.activityStatus === '2') {
						console.log(item.price);
						item.lopt1 = (item.goodsNum * item.price).toFixed(2)
						// item.lopt1.toFixed(2)
						// Number(item.lopt1)
					} else {
						// console.log();
					}
					// if (item.selected === true) {
					// 	this.data.lrrnu += Number(item.goodsNum)
					// }
				}
				let lrrnu = 0
				res.data.data.shoppingCartList.forEach((xres, index) => {
					if (xres.selected === true) {
						lrrnu += Number(xres.goodsNum);
					}
					if (xres.inventory > 0) {} else {
						xres.selected = false
					}
				})
				// console.log(lrrnu);
				// console.log(this.data.lrrnu);
				// console.log(cartGroupData);
				this.setData({
					isNotEmpty: true,
					cartGroupData: res.data.data.shoppingCartList,
					numdata: lrrnu
				})

				// let add = []
				// let vin = wx.getStorageSync('vin')
				// res.data.data.shoppingCartList.forEach((r, i) => {
				// 	add.push({
				// 		price: r.price,
				// 		num: r.goodsNum,
				// 		vin: vin,
				// 		productNumber: r.productNumber,
				// 		cartId: r.cartId
				// 	})
				// })
				// console.log(add);
				// let arr = JSON.stringify(add)
				// wx.request({
				// 	url: app.globalData.Baseurl + '/api/shopping/updateCart',
				// 	method: 'POST',
				// 	data: {
				// 		param: arr
				// 	},
				// 	header: {
				// 		'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
				// 	},
				// 	success: xite => {
				// 		console.log(xite);
				// 		// do something
				// 	},
				// 	fail: err => {}
				// })
			} else {
				console.log('sssss');
				let listdata = []
				this.setData({
					isNotEmpty: false,
					cartGroupData: listdata
				})
			}
		})
		// this.getCartGroupData().then((res) => {
		// 	let isEmpty = true;
		// 	const cartGroupData = res.data;
		// 	// 一些组件中需要的字段可能接口并没有返回，或者返回的数据结构与预期不一致，需要在此先对数据做一些处理
		// 	// 统计门店下加购的商品是否全选、是否存在缺货/无货
		// 	for (const store of cartGroupData.storeGoods) {
		// 		store.isSelected = true; // 该门店已加购商品是否全选
		// 		store.storeStockShortage = false; // 该门店已加购商品是否存在库存不足
		// 		if (!store.shortageGoodsList) {
		// 			store.shortageGoodsList = []; // 该门店已加购商品如果库存为0需单独分组
		// 		}
		// 		for (const activity of store.promotionGoodsList) {
		// 			activity.goodsPromotionList = activity.goodsPromotionList.filter((goods) => {
		// 				goods.originPrice = undefined;

		// 				// 统计是否有加购数大于库存数的商品
		// 				if (goods.quantity > goods.stockQuantity) {
		// 					store.storeStockShortage = true;
		// 				}
		// 				// 统计是否全选
		// 				if (!goods.isSelected) {
		// 					store.isSelected = false;
		// 				}
		// 				// 库存为0（无货）的商品单独分组
		// 				if (goods.stockQuantity > 0) {
		// 					return true;
		// 				}
		// 				store.shortageGoodsList.push(goods);
		// 				return false;
		// 			});

		// 			if (activity.goodsPromotionList.length > 0) {
		// 				isEmpty = false;
		// 			}
		// 		}
		// 		if (store.shortageGoodsList.length > 0) {
		// 			isEmpty = false;
		// 		}
		// 	}
		// 	cartGroupData.invalidGoodItems = cartGroupData.invalidGoodItems.map((goods) => {
		// 		goods.originPrice = undefined;
		// 		return goods;
		// 	});
		// 	cartGroupData.isNotEmpty = !isEmpty;
		// 	this.setData({
		// 		cartGroupData
		// 	});
		// });
	},

	findGoods(spuId, skuId) {
		let currentStore;
		let currentActivity;
		let currentGoods;
		const {
			storeGoods
		} = this.data.cartGroupData;
		for (const store of storeGoods) {
			for (const activity of store.promotionGoodsList) {
				for (const goods of activity.goodsPromotionList) {
					if (goods.spuId === spuId && goods.skuId === skuId) {
						currentStore = store;
						currentActivity = currentActivity;
						currentGoods = goods;
						return {
							currentStore,
							currentActivity,
							currentGoods,
						};
					}
				}
			}
		}
		return {
			currentStore,
			currentActivity,
			currentGoods,
		};
	},
	findGoods2(cartId, skuId) {
		// let currentGoods;
		console.log('sssssssssss');
		const {
			cartGroupData
		} = this.data;
		let listdata = []
		// this.data.numshuzi = ''
		this.setData({
			numshuzi: ''
		})
		// for (let store of cartGroupData) {
		// if (store.cartId === cartId) {
		// 	store.selected = !store.selected;
		// 	listdata.push(store)
		// 	console.log(store.selected);
		// 	if (store.selected === false) {
		// 		let sel = this.data.seleid = false
		// 		console.log(sel);
		// 		this.setData({
		// 			seleid: sel
		// 		})
		let numlist = []
		cartGroupData.forEach((item, index) => {
			console.log(cartGroupData[index].selected);
			if (item.cartId === cartId) {
				cartGroupData[index].selected = !item.selected
				listdata.push(item)
				console.log(cartGroupData);
				this.setData({
					cartGroupData
				})
			}
			if (item.selected === true) {
				numlist.push(item)
			}

		})
		// let numshuzi = '',

		if (numlist.length === 1) {
			this.setData({
				numdata: numlist[0].goodsNum
			})
		} else {
			let run = 0
			numlist.forEach((res, index) => {
				// console.log(typeof res.goodsNum);
				run += Number(res.goodsNum)
				// Number(res.goodsNum)
			})
			console.log(run);
			// let arr = numlist.reduce((sum, item) => sum + item.value, 0)
			console.log(arr);
			this.setData({
				numdata: run
			})
		}

		console.log(numlist);
		// console.log(cartGroupData);
		// console.log(listdata);
		// if (listdata[0].selected === false) {
		let arr = cartGroupData.some((item, index) => {
			console.log(item.selected);
			item.selected === false
			return item.selected === false
		})
		console.log(arr);
		if (arr === true) {
			let sel = this.data.seleid = false
			console.log(sel);
			this.setData({
				seleid: sel
			})
		} else {
			let sel = this.data.seleid = true
			console.log(sel);
			this.setData({
				seleid: sel
			})
		}
		// if (item.selected === false) {
		// 	let sel = this.data.seleid = false
		// 	console.log(sel);
		// 	this.setData({
		// 		seleid: sel
		// 	})
		// }

		// }
		// if (cartGroupData[index].selected === true) {
		// 	let sel = this.data.seleid = true
		// 	console.log(sel);
		// 	this.setData({
		// 		seleid: sel
		// 	})
		// }
		// }
		// else {
		// 	let sel = this.data.seleid = true
		// 	this.setData({
		// 		seleid: sel
		// 	})
		// }
		// 	}
		// }
		// this.setData({
		// 	cartGroupData: cartGroupData,
		// })
		// for (const store of storeGoods) {
		// 	for (const activity of store.promotionGoodsList) {
		// 		for (const goods of activity.goodsPromotionList) {
		// 			if (goods.spuId === spuId && goods.skuId === skuId) {
		// 				currentStore = store;
		// 				currentActivity = currentActivity;
		// 				currentGoods = goods;
		// 				return {
		// 					currentStore,
		// 					currentActivity,
		// 					currentGoods,
		// 				};
		// 			}
		// 		}
		// 	}
		// }
		// return {
		// 	currentStore,
		// 	currentActivity,
		// 	currentGoods,
		// };
	},

	// 注：实际场景时应该调用接口获取购物车数据
	getCartGroupData() {
		const {
			cartGroupData
		} = this.data;
		if (!cartGroupData) {
			return fetchCartGroupData();
		}
		return Promise.resolve({
			data: cartGroupData
		});
	},

	// 选择单个商品
	// 注：实际场景时应该调用接口更改选中状态
	selectGoodsService({
		cartId,
		skuId,
		selected
	}) {
		this.findGoods2(cartId, skuId).currentGoods.selected = selected;
		return Promise.resolve();
	},

	// 全选门店
	// 注：实际场景时应该调用接口更改选中状态
	selectStoreService({
		storeId,
		isSelected
	}) {
		const currentStore = this.data.cartGroupData.storeGoods.find((s) => s.storeId === storeId);
		currentStore.isSelected = isSelected;
		currentStore.promotionGoodsList.forEach((activity) => {
			activity.goodsPromotionList.forEach((goods) => {
				goods.isSelected = isSelected;
			});
		});
		return Promise.resolve();
	},

	// 加购数量变更
	// 注：实际场景时应该调用接口
	changeQuantityService({
		spuId,
		skuId,
		quantity
	}) {
		this.findGoods(spuId, skuId).currentGoods.quantity = quantity;
		return Promise.resolve();
	},

	// 删除加购商品
	// 注：实际场景时应该调用接口
	deleteGoodsService({
		spuId,
		skuId
	}) {
		console.log('ssssssssssssssssss');

		function deleteGoods(group) {
			for (const gindex in group) {
				const goods = group[gindex];
				if (goods.spuId === spuId && goods.skuId === skuId) {
					group.splice(gindex, 1);
					return gindex;
				}
			}
			return -1;
		}
		const {
			storeGoods,
			invalidGoodItems
		} = this.data.cartGroupData;
		for (const store of storeGoods) {
			for (const activity of store.promotionGoodsList) {
				if (deleteGoods(activity.goodsPromotionList) > -1) {
					return Promise.resolve();
				}
			}
			if (deleteGoods(store.shortageGoodsList) > -1) {
				return Promise.resolve();
			}
		}
		if (deleteGoods(invalidGoodItems) > -1) {
			return Promise.resolve();
		}
		return Promise.reject();
	},

	// 清空失效商品
	// 注：实际场景时应该调用接口
	clearInvalidGoodsService() {
		this.data.cartGroupData.invalidGoodItems = [];
		return Promise.resolve();
	},

	onGoodsSelect(e) {
		console.log(e);
		if (e.detail.goods.inventory <= 0) {

		} else {
			const {
				goods: {
					cartId,
					skuId
				},
				selected,
			} = e.detail;
			// if (this.data.seleid === 0) {
			this.findGoods2(cartId, skuId);
		}

		// } else {

		// }

		// const {
		// 	currentGoods
		// } =
		// Toast({
		// 	context: this,
		// 	selector: '#t-toast',
		// 	message: `${selected ? '选择' : '取消'}`,
		// 	icon: '',
		// });
		// this.selectGoodsService({
		// 	cartId,
		// 	skuId,
		// 	selected
		// }).then(() => this.refreshData());
	},

	onStoreSelect(e) {
		const {
			store: {
				storeId
			},
			selected,
		} = e.detail;
		this.selectStoreService({
			storeId,
			selected
		}).then(() => this.refreshData());
	},

	onQuantityChange(e) {
		console.log(e);
		const {
			goods: {
				spuId,
				skuId
			},
			quantity,
		} = e.detail;
		let arr = e.detail
		// this.setData({
		// 	numquantity: arr
		// })
		// console.log(this.data.numquantity);
		this.data.cartGroupData.forEach((r, i) => {
			// console.log(r);
			if (r.selected === true) {
				if (r.productNumber === arr.goods.productNumber) {}
			}
			// console.log(arr.quantity);
			if (arr.goods.conId === r.conId) {
				if (arr.quantity > r.inventory) {
					Toast({
						context: this,
						selector: '#t-toast',
						message: '当前商品库存不足',
					});
					r.selected = false
					this.setData({
						cartGroupData: this.data.cartGroupData
					})
					return;
				} else {
					let stnum = e.detail.quantity
					r.goodsNum = stnum.toString()
					this.jisuande(arr)
				}
			}
			return
		})
		// console.log(arr);
		// let numdata = ''
		// if (arr.goods.activityStatus === '1') {
		// 	numdata = arr.goods.price
		// } else {
		// 	numdata = arr.goods.price
		// }


		// wx.request({
		// 	url: 'http://111.203.231.105:801/api/order/orderCalculate',
		// 	method: 'POST',
		// 	data: {
		// 		vin: vin,
		// 		num: arr.quantity,
		// 		price: arr.goods.price,
		// 		productNumber: arr.goods.productNumber
		// 	},
		// 	header: {
		// 		'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
		// 	},
		// 	success: xite => {
		// 		console.log(xite);
		// 		this.setData({
		// 			sum: xite.data.data.zongPrice,
		// 			// danpinjiage: xite.data.data.zongPrice
		// 		})
		// 		this.data.cartGroupData.forEach((res, index) => {
		// 			if (arr.goods.cartId === res.cartId) {
		// 				if (arr.goods.activityStatus === '1') {
		// 					res.lopt1 = xite.data.data.zongPrice
		// 					res.itemYuanTotalPrice = xite.data.data.itemYuanTotalPrice
		// 				} else if (arr.goods.activityStatus === '2') {
		// 					res.lopt1 = xite.data.data.zongPrice,
		// 						res.itemYuanTotalPrice = xite.data.data.itemYuanTotalPrice
		// 				} else {
		// 					// console.log();
		// 				}
		// 			}
		// 		})
		// 		this.setData({
		// 			cartGroupData: this.data.cartGroupData
		// 		})
		// 		// do something
		// 	},
		// 	fail: err => {}
		// })

		// console.log(add);
		// let lista = JSON.stringify(add)
		// wx.request({
		// 	url: 'http://111.203.231.105:801/api/shopping/updateCart',
		// 	method: 'POST',
		// 	data: {
		// 		param: lista,
		// 		vin: vin,
		// 	},
		// 	header: {
		// 		'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
		// 	},
		// 	success: xite => {
		// 		console.log(xite);
		// 		this.setData({
		// 			numquantity: xite.data.data.discountTotalPrices
		// 		})
		// 		let sunm = []
		// 		this.data.cartGroupData.forEach((res, index) => {
		// 			if (res.selected === false) {
		// 				console.log(quantity);
		// 				if (quantity === '') {
		// 					// console.log();
		// 				} else {
		// 					if (res.cartId === arr.goods.cartId) {
		// 						res.goodsNum = quantity
		// 					}
		// 					// console.log();
		// 				}
		// 				// sunm+=
		// 			} else {
		// 				sunm.push(Number(res.goodsNum))
		// 				let add = sunm.reduce(function (sum, number) {
		// 					return sum + number
		// 				})
		// 				// console.log(Number(res.goodsNum));
		// 				console.log(add);
		// 				this.setData({
		// 					numdata: add
		// 				})
		// 				// this.dibushuliang()
		// 			}
		// 		})
		// 		// do something
		// 	},
		// 	fail: err => {}
		// })



		// const {
		// 	currentGoods
		// } = this.findGoods(spuId, skuId);
		// const stockQuantity = currentGoods.stockQuantity > 0 ? currentGoods.stockQuantity : 0; // 避免后端返回的是-1
		// // 加购数量超过库存数量
		// if (quantity > stockQuantity) {
		// 	// 加购数量等于库存数量的情况下继续加购
		// 	if (currentGoods.quantity === stockQuantity && quantity - stockQuantity === 1) {
		// 		Toast({
		// 			context: this,
		// 			selector: '#t-toast',
		// 			message: '当前商品库存不足',
		// 		});
		// 		return;
		// 	}
		// 	Dialog.confirm({
		// 			title: '商品库存不足',
		// 			content: `当前商品库存不足，最大可购买数量为${stockQuantity}件`,
		// 			confirmBtn: '修改为最大可购买数量',
		// 			cancelBtn: '取消',
		// 		})
		// 		.then(() => {
		// 			this.changeQuantityService({
		// 				spuId,
		// 				skuId,
		// 				quantity: stockQuantity,
		// 			}).then(() => this.refreshData());
		// 		})
		// 		.catch(() => {});
		// 	return;
		// }
		// this.changeQuantityService({
		// 	spuId,
		// 	skuId,
		// 	quantity
		// }).then(() => this.refreshData());
	},
	jisuande(arr) {
		let vin = wx.getStorageSync('vin')
		let login = wx.getStorageSync('login')
		let add = []
		console.log(arr);
		this.data.cartGroupData.forEach((r, i) => {
			console.log(r);
			if (r.selected === true) {
				if (r.productNumber === arr.goods.productNumber) {

				}
				add.push({
					price: r.price,
					num: r.goodsNum,
          vin: vin || '1234567890',
					productNumber: r.productNumber,
					cartId: r.cartId,
					shippingSpace: r.shippingSpace,
					memberNumber: login.data.data.memberNumber,
					goodsId: r.goodsId
				})
			}
		})
		wx.request({
			url: app.globalData.baseApiUrl +'/api/order/orderCalculate',
			method: 'POST',
			data: {
        vin: vin || '1234567890',
				num: arr.quantity,
				price: arr.goods.price,
				productNumber: arr.goods.productNumber
			},
			header: {
				'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
			},
			success: xite => {
				console.log(xite);
				this.setData({
					sum: xite.data.data.zongPrice,
					// danpinjiage: xite.data.data.zongPrice
				})
				this.data.cartGroupData.forEach((res, index) => {
					if (arr.goods.cartId === res.cartId) {
						if (arr.goods.activityStatus === '1') {
							res.lopt1 = xite.data.data.zongPrice
							res.itemYuanTotalPrice = xite.data.data.itemYuanTotalPrice
						} else if (arr.goods.activityStatus === '2') {
							res.lopt1 = xite.data.data.zongPrice,
								res.itemYuanTotalPrice = xite.data.data.itemYuanTotalPrice
						} else {
							// console.log();
						}
					}
				})
				this.setData({
					cartGroupData: this.data.cartGroupData
				})
				// do something
			},
			fail: err => {}
		})

		console.log(add);
		let lista = JSON.stringify(add)
		wx.request({
			url: app.globalData.baseApiUrl +'/api/shopping/updateCart',
			method: 'POST',
			data: {
				param: lista,
        vin: vin || '1234567890',
			},
			header: {
				'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
			},
			success: xite => {
				console.log(xite);
				this.setData({
					numquantity: xite.data.data.discountTotalPrices
				})
				let sunm = []
				this.data.cartGroupData.forEach((res, index) => {
					if (res.selected === false) {
						console.log(quantity);
						if (quantity === '') {
							// console.log();
						} else {
							if (res.cartId === arr.goods.cartId) {
								res.goodsNum = quantity
							}
							// console.log();
						}
						// sunm+=
					} else {
						sunm.push(Number(res.goodsNum))
						let add = sunm.reduce(function (sum, number) {
							return sum + number
						})
						// console.log(Number(res.goodsNum));
						console.log(add);
						this.setData({
							numdata: add
						})
						// this.dibushuliang()
					}
				})
				// do something
			},
			fail: err => {}
		})
	},
	goCollect() {
		/** 活动肯定有一个活动ID，用来获取活动banner，活动商品列表等 */
		const promotionID = '123';
		wx.navigateTo({
			url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
		});
	},

	goGoodsDetail(e) {
		const {
			spuId,
			storeId
		} = e.detail.goods;
		wx.navigateTo({
			url: `/pages/goods/details/index?spuId=${spuId}&storeId=${storeId}`,
		});
	},

	clearInvalidGoods() {
		// 实际场景时应该调用接口清空失效商品
		this.clearInvalidGoodsService().then(() => this.refreshData());
	},

	onGoodsDelete(e) {
		const {
			goods: {
				sizeId,
				cartId
			},
		} = e.detail;
		Dialog.confirm({
			content: '确认删除该商品吗?',
			confirmBtn: '确定',
			cancelBtn: '取消',
		}).then(() => {
			// this.deleteGoodsService({
			// 	spuId,
			// 	skuId
			// }).then(() => {
			// 	Toast({
			// 		context: this,
			// 		selector: '#t-toast',
			// 		message: '商品删除成功'
			// 	});
			// 	this.refreshData();
			// });
			let userid = wx.getStorageSync('login')
			// console.log('qingkong');
			$api.deleteShoppingCartList({
				memberNumber: userid.data.data.memberNumber,
				source: 2,
				cartId: cartId
			}).then((res) => {
				console.log(res);
				// if (res.data) {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '商品删除成功'
				});

				this.refreshData();
				// let opn = 0
				// this.data.cartGroupData.forEach((item, index) => {
				// 	console.log(opn += Number(item.goodsNum));
				// })
				// } else {

				// }
			})
		});
	},

	onSelectAll(event) {
		console.log(event);
		const {
			isselected
		} = event?.detail ?? {};
		Toast({
			context: this,
			selector: '#t-toast',
			message: `${isselected ? '取消' : '点击'}了全选按钮`,
		});
		// 调用接口改变全选
		// console.log(event);
		let lonum = []
		this.data.cartGroupData.forEach((item, index) => {
			if (item.inventory <= 0) {
				item.selected = false
				lonum.push(item)
			}
		})
		console.log(lonum);
		if (lonum.length === 0) {
			let listdata = []
			let add = this.data.cartGroupData.some((item, index) => {
				return item.selected === false
			})
			console.log(add);
			if (add === true) {
				this.data.cartGroupData.forEach((item, index) => {
					item.selected = true
					listdata.push(item)
					this.setData({
						seleid: true,
						isselected: true
					})
					this.dibushuliang()
				})
			} else {
				this.data.cartGroupData.forEach((item, index) => {
					item.selected = false
					listdata.push(item)
					this.setData({
						seleid: false,
						isselected: false,
						numdata: 0
					})
				})
			}
		} else {

		}

		// this.data.cartGroupData.forEach((item, index) => {
		// 	if (item.selected === true) {
		// 		if (isselected === false) {
		// 			item.selected = true
		// 			this.setData({
		// 				seleid: true
		// 			})
		// 		} else {
		// 			item.selected = false
		// 			this.setData({
		// 				seleid: false
		// 			})
		// 		}
		// 	} else {
		// 		if (isselected === false) {
		// 			item.selected = true
		// 			this.setData({
		// 				seleid: false
		// 			})
		// 		} else {
		// 			item.selected = false
		// 			this.setData({
		// 				seleid: true
		// 			})
		// 		}
		// 	}

		// 	console.log(item.selected);
		// 	listdata.push(item)
		// })
		console.log(listdata);
		this.setData({
			cartGroupData: listdata
		})
	},

	onToSettle() {
		let userInfo = wx.getStorageSync('userTel')
		if (userInfo) {
			// const goodsRequestList = [];
			// this.data.cartGroupData.storeGoods.forEach((store) => {
			// 	store.promotionGoodsList.forEach((promotion) => {
			// 		promotion.goodsPromotionList.forEach((m) => {
			// 			if (m.isSelected == 1) {
			// 				goodsRequestList.push(m);
			// 			}
			// 		});
			// 	});
			// });
			// wx.setStorageSync('order.goodsRequestList', JSON.stringify(goodsRequestList));
			// let listdata = []
			// this.data.cartGroupData.forEach((item, index) => {
			// 	item.selected = true
			// 	console.log(item);
			// 	listdata.push(item)
			// })
			// this.setData({
			// 	cartGroupData: listdata
			// })
			// console.log(this.data.cartGroupData);
			let listdata = []
			this.data.cartGroupData.forEach((item, index) => {
				if (item.selected === true) {
					listdata.push(item)
					this.dibushuliang()
				}
			})
			console.log(listdata);
			let opt = JSON.stringify(listdata)
			if (listdata.length > 0) {
				wx.navigateTo({
					url: `/packageA/pages/Shoppingbuying/index?data=${opt}&index=1`
				});
			}

		} else {
			wx.showToast({ // 显示加载中loading效果
				title: "请您先登录",
				icon: 'error',
				duration: 2000,
				success: function () {
					console.log('sssssssssssssssssssssssssssssssss');
					setTimeout(() => {
						wx.navigateTo({
							url: '/pages/usercenter/userAuthorized/Authorized'
						})
					}, 2000)
				}
			});
		}
	},
	onGotoHome() {
		wx.navigateBack({
			delta: 2
		})
	},
	empty() {
		let userid = wx.getStorageSync('login')
		// console.log('qingkong');
		$api.deleteShoppingCartListquanbu({
			memberNumber: userid.data.data.memberNumber,
			source: 2
		}).then((res) => {
			console.log(res);
			Toast({
				context: this,
				selector: '#t-toast',
				message: '商品全部删除成功'
			});
			this.refreshData();
			// if (res) {

			// } else {
			// 	this.refreshData();
			// }
		})
	},
	dibushuliang() {
		let login = wx.getStorageSync('login')
		let vin = wx.getStorageSync('vin')
		$api.shoppingCartList({
			memberNumber: login.data.data.memberNumber,
			page: 1,
			size: 10000,
			source: "2",
			vin: vin || '1234567890',
		}).then((res) => {
			console.log(res);
			// console.log(res.data);
			// let isEmpty = true;
			this.setData({
				numdata: res.data.data.goodsTotal
			})
		})
	}
});
